<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>Customer page</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <script type="text/javascript" src="common.js"></script>
    <script type="text/javascript">

        var logged_username = "";

        // Initialize the producer select box
        function initCentreSelect() {
            removeAllSelectOptions("centre_code");
            addSelectOptions("centre_code", "Choose one", "centre");
            ajaxCall('getcentres.php', loadCentresFromXml);
        }

        // Parse the XML file containing the list of producers and add
        // to the producer select box
        function loadCentresFromXml(xml_doc) {
            xmlDoc=loadXMLString(xml_doc);
            
            centre_list=xmlDoc.documentElement.getElementsByTagName("centre");
            for (i=0;i<centre_list.length;i++) {
                centre_code=centre_list[i].getElementsByTagName("code");
                centre_name=centre_list[i].getElementsByTagName("name");
                try {
                    code = centre_code[0].firstChild.nodeValue;
                    name = centre_name[0].firstChild.nodeValue;
                    addSelectOptions("centre_code", code, name);
                } catch (er) {
                    code = 1;
                    name = '';
                }
            }
            initFilmSelect();
        }

        // Initialize the producer select box
        function initAgentSelect(centre_code, agent_code) {
            removeAllSelectOptions("agent_code");
            addSelectOptions("agent_code", "Choose one", "agent");
            ajaxCall('getagents.php?centre_code='+centre_code, loadAgentsFromXml, agent_code);
        }

        // Parse the XML file containing the list of producers and add
        // to the producer select box
        function loadAgentsFromXml(xml_doc, agent_code) {
            xmlDoc=loadXMLString(xml_doc);

            agent_list=xmlDoc.documentElement.getElementsByTagName("agent");
            for (i=0;i<agent_list.length;i++) {
                agent_code=agent_list[i].getElementsByTagName("code");
                agent_name=agent_list[i].getElementsByTagName("name");
                try {
                    code = agent_code[0].firstChild.nodeValue;
                    name = agent_name[0].firstChild.nodeValue;
                    addSelectOptions("agent_code", code, name);
                } catch (er) {
                    code = 1;
                    name = '';
                }
            }
            
            if(logged_username) {
                selectOptionValue("agent_code",logged_username);
            } else {
                selectOptionValue("agent_code",agent_code);
            }

        }

        // Initialize the producer select box
        function initFilmSelect() {
            removeAllSelectOptions("film_code");
            addSelectOptions("film_code", "Choose one", "film");
            ajaxCall('getfilms.php', loadFilmsFromXml);
        }

        // Parse the XML file containing the list of producers and add
        // to the producer select box
        function loadFilmsFromXml(xml_doc) {
            xmlDoc=loadXMLString(xml_doc);

            film_list=xmlDoc.documentElement.getElementsByTagName("film");
            for (i=0;i<film_list.length;i++) {
                film_code=film_list[i].getElementsByTagName("code");
                film_name=film_list[i].getElementsByTagName("name");
                try {
                    code = film_code[0].firstChild.nodeValue;
                    name = film_name[0].firstChild.nodeValue;
                    addSelectOptions("film_code", code, name);
                } catch (er) {
                    code = 1;
                    name = '';
                }
            }
            initTicketSelect();
        }

        // Initialize the producer select box
        function initTicketSelect() {
            removeAllSelectOptions("ticket_rate");
            addSelectOptions("ticket_rate", "Choose one", "ticket rate");
            ajaxCall('gettickets.php', loadTicketsFromXml);
        }

        // Parse the XML file containing the list of producers and add
        // to the producer select box
        function loadTicketsFromXml(xml_doc) {
            xmlDoc=loadXMLString(xml_doc);

            ticket_list=xmlDoc.documentElement.getElementsByTagName("ticket");
            for (i=0;i<ticket_list.length;i++) {
                ticket_rate=ticket_list[i].getElementsByTagName("rate");
                coupon_value=ticket_list[i].getElementsByTagName("coupon_value");
                try {
                    rate = ticket_rate[0].firstChild.nodeValue;
                    value = coupon_value[0].firstChild.nodeValue;
                    addSelectOptions("ticket_rate", rate, value);
                } catch (er) {
                    rate = 20;
                    value = '';
                }
            }
            getAgentCodeCookie();
        }

        function saveRespHdlr(xml_doc) {
            //TODO: Handle the response from the server for the save command
            xmlDoc=loadXMLString(xml_doc);

            result=xmlDoc.getElementsByTagName("result");
            try {
                result_text = result[0].firstChild.nodeValue;
            } catch (er) {
                result_text = "Error";
            }
            switch (result_text.toLowerCase()) {
                case "added":
                    alert("Ticket added!")
                    break;
                case "updated":
                    alert("Ticket updated");
                    break;
                case "login":
                    alert("Login required. You will now be redirected to the login page");
                    window.location="login.html";
                    break;
                case "error":
                default:
                    alert("Error occured while processing the save response");
                    break;
            }
        }

        function queryRespHdlr(xml_doc) {
            //TODO: Handle the response from the server for the save command
            xmlDoc=loadXMLString(xml_doc);

            result=xmlDoc.getElementsByTagName("result");
            try {
                result_text = result[0].firstChild.nodeValue;
            } catch (er) {
                result_text = "Error";
            }
            switch (result_text.toLowerCase()) {
                case "deleted":
                    alert('Deleted ticket');
                    resetForm();
                    break;
                case "success":
                    //alert('Successful query');
                    try {
                        cust_name=xmlDoc.getElementsByTagName("cust_name");
                        document.getElementById("cust_name").value=cust_name[0].firstChild.nodeValue;
                        centre_code=xmlDoc.getElementsByTagName("centre_code");
                        selectOptionValue("centre_code", centre_code[0].firstChild.nodeValue);
                        agent_code=xmlDoc.getElementsByTagName("agent_code");
                        selectOptionValue("agent_code", agent_code[0].firstChild.nodeValue);
                        initAgentSelect(centre_code[0].firstChild.nodeValue, agent_code[0].firstChild.nodeValue);
                        film_code=xmlDoc.getElementsByTagName("film_code");
                        selectOptionValue("film_code", film_code[0].firstChild.nodeValue);
                        ticket_no=xmlDoc.getElementsByTagName("ticket_no");
                        document.getElementById("ticket_no").value=ticket_no[0].firstChild.nodeValue;
                        phone=xmlDoc.getElementsByTagName("phone");
                        document.getElementById("phone").value=phone[0].firstChild.nodeValue;
                        date_issue=xmlDoc.getElementsByTagName("date_issue");
                        document.getElementById("date_issue").value=date_issue[0].firstChild.nodeValue;
                        time_issue=xmlDoc.getElementsByTagName("time_issue");
                        document.getElementById("time_issue").value=time_issue[0].firstChild.nodeValue;
                        ticket_rate=xmlDoc.getElementsByTagName("ticket_rate");
                        selectOptionValue("ticket_rate", ticket_rate[0].firstChild.nodeValue);
                    } catch (er) {
                        alert('Invalid ticket number');
                    }
                    break;
                case "login":
                    alert("Login required. You will now be redirected to the login page");
                    window.location="login.html";
                    break;
                case "error":
                default:
                    alert("Error occured while processing the query response");
                    break;
            }
        }

        function dispTableHdlr(xml_doc) {
            //TODO: Handle the response from the server for the save command
            xmlDoc=loadXMLString(xml_doc);

            result=xmlDoc.getElementsByTagName("result");
            try {
                result_text = result[0].firstChild.nodeValue;
            } catch (er) {
                result_text = "Error";
            }
            table_text = "<button onclick='displayTable()'>Display the customer table</button>";
            table_text = table_text + "<table border='1'>"+
                      "<tr><th>ID</th>"+
                          "<th>Customer Name</th>"+
                          "<th>Centre Code</th>"+
                          "<th>Agent Code</th>"+
                          "<th>Film Code</th>"+
                          "<th>Ticket number</th>"+
                          "<th>Phone</th>"+
                          "<th>Date of Issue</th>"+
                          "<th>Time of Issue</th>"+
                          "<th>Ticket rate</th>"+
                          "<th>Status</th></tr>";

            switch (result_text.toLowerCase()) {
                case "success":
                    //alert('Successful query');
                    agents=xmlDoc.getElementsByTagName("customer");
                    for (i=0;i<agents.length;i++) {
                        table_text = table_text + "<tr>";
                        try {
                            table_text = table_text + insertField(agents[i], "cust_id");
                            table_text = table_text + insertField(agents[i], "cust_name");
                            table_text = table_text + insertField(agents[i], "centre_code");
                            table_text = table_text + insertField(agents[i], "agent_code");
                            table_text = table_text + insertField(agents[i], "film_code");
                            table_text = table_text + insertField(agents[i], "ticket_no");
                            table_text = table_text + insertField(agents[i], "phone");
                            table_text = table_text + insertField(agents[i], "date_issue");
                            table_text = table_text + insertField(agents[i], "time_issue");
                            table_text = table_text + insertField(agents[i], "ticket_rate");
                            table_text = table_text + insertField(agents[i], "value");
                        } catch (er) {
                            alert('Invalid ticket number');
                        }
                        table_text = table_text + "</tr>";
                    }
                    break;
                case "login":
                    alert("Login required. You will now be redirected to the login page");
                    window.location="login.html";
                    break;
                case "error":
                default:
                    alert("Error occured while processing the query response");
                    break;
            }

            table_text = table_text + "</table>";
            document.getElementById("cust_table").innerHTML=table_text;
        }

        function updateCodeHdlr(xml_doc) {
            //TODO: Handle the response from the server for the save command
            xmlDoc=loadXMLString(xml_doc);

            result=xmlDoc.getElementsByTagName("result");
            try {
                result_text = result[0].firstChild.nodeValue;
            } catch (er) {
                result_text = "Error";
            }
            switch (result_text.toLowerCase()) {
                case "success":
                    //alert('Successful query');
                    try {
                        centre_code=xmlDoc.getElementsByTagName("centre_code");
                        selectOptionValue("centre_code", centre_code[0].firstChild.nodeValue);
                        initAgentSelect(centre_code[0].firstChild.nodeValue);
                    } catch (er) {
                        alert('Invalid agent code');
                    }
                    break;
                case "login":
                    alert("Login required. You will now be redirected to the login page");
                    window.location="login.html";
                    break;
                case "error":
                default:
                    alert("Error occured while processing the query response");
                    break;
            }
        }

        function validateForm() {
            var return_value = true;
            var cust_name=document.getElementById("cust_name").value;
            var centres=document.getElementById("centre_code");
            var agents=document.getElementById("agent_code");
            var films=document.getElementById("film_code");
            var ticket_no=document.getElementById("ticket_no").value;
            var phone=document.getElementById("phone").value;
            var date_issue=document.getElementById("date_issue").value;
            var time_issue=document.getElementById("time_issue").value;
            var ticket_rates=document.getElementById("ticket_rate");

            if (!cust_name) {
                document.getElementById("cust_name_hint").innerHTML =
                    "<p style='color:red'><b>Name cannot be empty </b></p>";
                return_value = false;
            } else {
                document.getElementById("cust_name_hint").innerHTML = " ";
            }

            if (!centres.selectedIndex) {
                document.getElementById("centre_code_hint").innerHTML =
                    "<p style='color:red'><b>Centre Code should be selected</b></p>";
                return_value = false;
            } else {
                document.getElementById("centre_code_hint").innerHTML = " ";
            }
            
            if (!agents.selectedIndex) {
                document.getElementById("agent_code_hint").innerHTML =
                    "<p style='color:red'><b>Agent Code should be selected</b></p>";
                return_value = false;
            } else {
                document.getElementById("agent_code_hint").innerHTML = " ";
            }

            if (!films.selectedIndex) {
                document.getElementById("film_code_hint").innerHTML =
                    "<p style='color:red'><b>Film Code should be selected</b></p>";
                return_value = false;
            } else {
                document.getElementById("film_code_hint").innerHTML = " ";
            }

            if (isNaN(phone) || !phone) {
                document.getElementById("phone_hint").innerHTML = 
                    "<p style='color:red'><b>Invalid phone number</b></p>";
                return_value = false;
            } else {
                document.getElementById("phone_hint").innerHTML = " ";
            }

            if (!ticket_no) {
                document.getElementById("ticket_no_hint").innerHTML =
                    "<p style='color:red'><b>Ticket number cannot be empty </b></p>";
                return_value = false;
            } else {
                document.getElementById("ticket_no_hint").innerHTML = " ";
            }

            if (!ticket_rates.selectedIndex) {
                document.getElementById("ticket_rate_hint").innerHTML =
                    "<p style='color:red'><b>Ticket rate must be selected </b></p>";
                return_value = false;
            } else {
                document.getElementById("ticket_rate_hint").innerHTML = " ";
            }

            return return_value;

        }

        function saveForm() {
            if (!validateForm()) {
                alert('Correct errors in the form before trying again!');
                return;
            }
            
            var cust_name=document.getElementById("cust_name").value;
            var centres=document.getElementById("centre_code");
            var selected_centre=centres.options[centres.selectedIndex].value;
            var agents=document.getElementById("agent_code");
            var selected_agent=agents.options[agents.selectedIndex].value;
            var films=document.getElementById("film_code");
            var selected_film=films.options[films.selectedIndex].value;
            var ticket_no=document.getElementById("ticket_no").value;
            var phone=document.getElementById("phone").value;
            var date_issue=document.getElementById("date_issue").value;
            var time_issue=document.getElementById("time_issue").value;
            var ticket_rates=document.getElementById("ticket_rate");
            var ticket_rate=ticket_rates.options[ticket_rates.selectedIndex].value;

            srvr_request="savecustomer.php?cust_name="+cust_name+
                            "&centre_code="+selected_centre+
                            "&agent_code="+selected_agent+
                            "&film_code="+selected_film+
                            "&ticket_no="+ticket_no+
                            "&phone="+phone+
                            "&date_issue="+date_issue+
                            "&time_issue="+time_issue+
                            "&ticket_rate="+ticket_rate;
                        
            ajaxCall(srvr_request,saveRespHdlr);
        }

        function resetForm() {
            document.getElementById("cust_name").value = "";
            document.getElementById("cust_name_hint").innerHTML = "";
            document.getElementById("ticket_no").value = "";
            document.getElementById("ticket_no_hint").innerHTML = "";
            document.getElementById("phone").value = "";
            document.getElementById("phone_hint").innerHTML = "";
            document.getElementById("date_issue").value = "";
            document.getElementById("date_issue_hint").innerHTML = "";
            document.getElementById("time_issue").value = "";
            document.getElementById("time_issue_hint").innerHTML = "";
            initCentreSelect();
            document.getElementById("centre_code_hint").innerHTML = "";
            document.getElementById("agent_code_hint").innerHTML = "";
            document.getElementById("ticket_rate_hint").innerHTML = "";
            document.getElementById("film_code_hint").innerHTML = "";
        }

        function queryForm() {
            var ticket_no=document.getElementById("ticket_no").value;
            srvr_request="querycustomer.php?ticket_no="+ticket_no;
            ajaxCall(srvr_request,queryRespHdlr);
        }

        function deleteForm() {
            var ticket_no=document.getElementById("ticket_no").value;
            srvr_request="querycustomer.php?ticket_no="+ticket_no+
                        "&action=delete";
            ajaxCall(srvr_request,queryRespHdlr);
        }

        function displayTable() {
            if(logged_username) {
                srvr_request="querycustomer.php?agent_code="+logged_username;
            } else {
                srvr_request="querycustomer.php";
            }
            ajaxCall(srvr_request,dispTableHdlr);
        }

        function getAgentCodeCookie() {
            session_auth=getCookie('session_auth');
            switch (session_auth.toLowerCase()) {
                case "agent_auth":ff
                    logged_username=getCookie('username');
                    srvr_request="queryagent.php?agent_code="+logged_username;
                    ajaxCall(srvr_request,updateCodeHdlr);
                    break;
            }
            

        }

        function nullHdlr() {
            return;
        }

        function testAddTickets() {
            for(i=12340200;i<12340300;i++) {
                srvr_request="savecustomer.php?cust_name=Sriram"+
                        "&centre_code=1"+
                        "&agent_code=KA01M0051&"+
                        "film_code=TN010001"+
                        "&ticket_no="+i+
                        "&phone=0765830964"+
                        "&date_issue="+
                        "&time_issue="+
                        "&ticket_rate=40";
                    ajaxCall(srvr_request,nullHdlr);
            }
        }

        function checkLoginCookie() {
            session_auth=getCookie('session_auth');
            logged_username=getCookie('username');

            if(!session_auth || !logged_username) {
                setCookie('redirector','customer.html',60*60*1000);
                window.location = "login.html";
            }
            initCentreSelect();
        }

    </script>

  </head>
  <body onLoad="checkLoginCookie()">
      <table border="1">
          <tr><td width="150"><a href="producer.html"><b>Producers</b></a></td>
              <td width="150"><a href="film.html"><b>Films</b></a></td>
              <td width="150"><a href="centre.html"><b>Centres</b></a></td>
              <td width="150"><a href="agent.html"><b>Agents</b></a></td>
              <td width="150"><a href="customer.html"><b>Tickets</b></a></td>
              <td width="150"><a href="ticket.html"><b>Ticket rate information</b></a></td>
              <td width="150"><a href="ticketstatus.html"><b>Status of ticket sales for each film</b></a></td>
              <td width="150"><a href="customerstatus.html"><b>Status for each customer</b></a></td>
              <td width="150"><a href="logout.php"><b>Logout</b></a></td></tr>
      </table>

        <table border="0">

            <tr><td><label>Customer Name&nbsp;*</label></td>
                <td><input id="cust_name" type=text size='20' value=''></td>
                <td><div id="cust_name_hint"> </div></td></tr>

            <tr><td><label>Centre Code&nbsp;*</label></td>
                <td><select id="centre_code" onchange='initAgentSelect(this.value)'><option value=''></option></select></td>
                <td><div id="centre_code_hint"> </div></td></tr>

            <tr><td><label>Agent Code&nbsp;*</label></td>
                <td><select id=agent_code><option value=''></option></select></td>
                <td><div id="agent_code_hint"> </div></td></tr>

            <tr><td><label>Film Code&nbsp;*</label></td>
                <td><select id=film_code><option value=''></option></select></td>
                <td><div id="film_code_hint"> </div></td></tr>

            <tr><td><label>Ticket number&nbsp;*</label></td>
                <td><input id="ticket_no" type=text size='20' value=''></td>
                <td><div id="ticket_no_hint"> </div></td></tr>

            <tr><td><label>Phone&nbsp;*</label></td>
                <td><input id="phone" type=text size='20' value=''></td>
                <td><div id="phone_hint"> </div></td></tr>

            <tr><td><label>Date of Issue</label></td>
                <td><input id="date_issue" type=text size='20' value=''></td>
                <td><div id="date_issue_hint"> </div></td></tr>

            <tr><td><label>Time of Issue</label></td>
                <td><input id="time_issue" type=text size='20' value=''></td>
                <td><div id="time_issue_hint"> </div></td></tr>

            <tr><td><label>Ticket Rate&nbsp;*</label></td>
                <td><select id=ticket_rate><option value=''></option></select></td>
                <td><div id="ticket_rate_hint"> </div></td></tr>

            <tr /> <tr /> <tr />
        </table>
        <table>
            <tr><td><input type="submit" onClick="saveForm()" value="Save" /></td>
                <td><input type="submit" onClick="resetForm()" value="Reset" /></td>
                <td><input type="submit" onClick="queryForm()" value="Query" /></td>
                <td><input type="submit" onClick="deleteForm()" value="Delete" /></td></tr>

        </table>
      <!--
        <div id="cust_table" type="hidden">
            <button type="hidden" onclick="displayTable()">Display the customer table</button>
        </div>

        
        <div id="test_button" type="hidden">
            <button type="hidden" onclick="testAddTickets()">Add test entries</button>
        </div>
      -->

  </body>
</html>
